define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/order_goods/index' + location.search,
                    //add_url: 'shop/order_goods/add',
                    //edit_url: 'shop/order_goods/edit',
                    //del_url: 'shop/order_goods/del',
                    multi_url: 'shop/order_goods/multi',
                    import_url: 'shop/order_goods/import',
                    table: 'shop_order_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                //pagination: false,
                //commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'order.date', title: __('Order.date'), operate: 'LIKE'},
                        {field: 'order.shippingtime', title: __('配送时间'), searchList: $.getJSON('shop/order_goods/datelist')},
                        //{field: 'order_sn', title: __('Order_sn'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'order_id', title: __('Order_id')},
                        //{field: 'goods_sn', title: __('Goods_sn'), operate: 'LIKE'},
                        {field: 'goods_id', title: __('商品'), searchList: $.getJSON('shop/goods/goodslist')},
                        //{field: 'goods_sku_id', title: __('Goods_sku_id')},
                        {field: 'title', title: __('Title'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'nums', title: __('Nums'), operate: false},
                        //{field: 'marketprice', title: __('Marketprice'), operate:'BETWEEN'},
                        //{field: 'price', title: __('Price'), operate:'BETWEEN'},
                        //{field: 'realprice', title: __('Realprice'), operate:'BETWEEN'},
                        //{field: 'salestate', title: __('Salestate'), searchList: {"0":__('Salestate 0'),"1":__('Salestate 1'),"2":__('Salestate 2'),"3":__('Salestate 3'),"4":__('Salestate 4'),"5":__('Salestate 5'),"6":__('Salestate 6')}, formatter: Table.api.formatter.normal, operate: false},
                        {field: 'attrdata', title: __('Attrdata'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'weight', title: __('Weight'), operate:'BETWEEN'},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            var options = table.bootstrapTable('getOptions');
            var queryParams = options.queryParams;
            var value = '';
            // 绑定TAB事件
            /*$('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                value = $(this).data("value");
            });*/
            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                if (typeof value=='string'){
                    date = value;
                }

                options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    if (value&&typeof (value)=='object') {
                        params.filter = JSON.stringify(value);
                        let op = {}
                        for (let i in value) {
                            op[i] = '=';
                        }
                        params.op = JSON.stringify(op);
                    }
                    params = queryParams.call(this, params);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });

            $('#excel_dc').click(function(){
                var ids = Table.api.selectedids(table);//获取选中列的id
                ids = ids.join(',');
                if (ids==''){
                    alert('请选择要打印的订单');
                    return;
                }
                /*if (value==''){
                    alert('请选择日期打印');
                    return;
                }*/
                $.ajax({
                    url: "shop/order_goods/print",
                    type: 'post',
                    dataType: 'json',
                    data: {date: value,ids:ids},
                    success: function (ret) {

                    }, error: function (e) {
                        //Backend.api.toastr.error(e.message);
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
